import type { BlogPost, Category, Tag, PaginatedResponse } from '~/types'

export const useApi = () => {
  const config = useRuntimeConfig()
  
  // 获取文章列表
  const getPosts = async (params?: {
    page?: number
    pageSize?: number
    category?: string
    tag?: string
    search?: string
    featured?: boolean
  }): Promise<PaginatedResponse<BlogPost>> => {
    const response = await $fetch<{
      success: boolean
      message: string
      data: BlogPost[]
      pagination: {
        total: number
        page: number
        pageSize: number
        totalPages: number
      }
    }>('/api/posts', {
      query: params
    })
    
    return {
      data: response.data || [],
      pagination: response.pagination ? {
        total: response.pagination.total || 0,
        page: response.pagination.page || 1,
        pageSize: response.pagination.pageSize || 10,
        totalPages: response.pagination.totalPages || 0
      } : undefined
    }
  }

  // 获取文章详情
  const getPost = async (slug: string): Promise<BlogPost> => {
    const response = await $fetch<{
      success: boolean
      message: string
      data: BlogPost
    }>(`/api/posts/${slug}`)
    return response.data
  }

  // 获取分类列表
  const getCategories = async (): Promise<Category[]> => {
    const response = await $fetch<{
      success: boolean
      message: string
      data: Category[]
    }>('/api/categories')
    return response.data || []
  }

  // 获取标签列表
  const getTags = async (): Promise<Tag[]> => {
    const response = await $fetch<{
      success: boolean
      message: string
      data: Tag[]
    }>('/api/tags')
    return response.data || []
  }

  // 获取热门文章
  const getPopularPosts = async (limit?: number): Promise<BlogPost[]> => {
    const response = await $fetch<{
      success: boolean
      message: string
      data: BlogPost[]
    }>('/api/posts/popular', {
      query: { limit }
    })
    return response.data || []
  }

  // 获取推荐文章
  const getFeaturedPosts = async (limit?: number): Promise<BlogPost[]> => {
    const response = await $fetch<{
      success: boolean
      message: string
      data: BlogPost[]
    }>('/api/posts/featured', {
      query: { limit }
    })
    return response.data || []
  }

  // 搜索文章
  const searchPosts = async (keyword: string, params?: {
    page?: number
    pageSize?: number
  }): Promise<PaginatedResponse<BlogPost>> => {
    const response = await $fetch<{
      success: boolean
      message: string
      data: BlogPost[]
      pagination: {
        total: number
        page: number
        pageSize: number
        totalPages: number
      }
    }>('/api/posts/search', {
      query: { q: keyword, ...params }
    })
    
    return {
      data: response.data || [],
      pagination: response.pagination ? {
        total: response.pagination.total || 0,
        page: response.pagination.page || 1,
        pageSize: response.pagination.pageSize || 10,
        totalPages: response.pagination.totalPages || 0
      } : {
        total: 0,
        page: 1,
        pageSize: 10,
        totalPages: 0
      }
    }
  }

  return {
    getPosts,
    getPost,
    getCategories,
    getTags,
    getPopularPosts,
    getFeaturedPosts,
    searchPosts
  }
}